package cn.com.enboga.musicbox.db
{
	import flash.data.SQLMode;
	
	import mx.collections.ArrayCollection;
	
	public class MusicBoxParameterDAO
	{
		public function MusicBoxParameterDAO()
		{
		}

		/** 创建MusicBoxParameter 表 */
		public static function createMusicBoxParameter():void
		{	
			var sql:String = 
					"CREATE TABLE IF NOT EXISTS MusicBoxParameter (" + 
					" pid INTEGER PRIMARY KEY AUTOINCREMENT, " + 
					" pname TEXT, " + 
					" pvalue TEXT )";
				
			LocalDbConnection.sqlExecute(sql, SQLMode.CREATE, false);
		}

		/** 添加数据  */
		public static function insertMusicBoxParameter(obj:Object):void{
			var sql:String = 
					"INSERT INTO MusicBoxParameter(pname,pvalue) VALUES (" + 
					"'" + obj.pname + "', " + 
					"'" + obj.pvalue + "')";
					
			LocalDbConnection.sqlExecute(sql, SQLMode.UPDATE, false);
		}
		
		/** 查询所有参数  */
		public static function selectMusicBoxParameter():Array{
			var sql:String = "SELECT * FROM MusicBoxParameter";					
			var result:SQLResult = LocalDbConnection.sqlExecute(sql, SQLMode.READ, true);
			
			return result;
		}
		
		/** 获得某个参数  */
		public static function getParameter(parameterName:String):String{
			var sql:String = 
				"SELECT * FROM MusicBoxParameter WHERE pname='" + parameterName + "'";					
			var result:SQLResult = LocalDbConnection.sqlExecute(sql, SQLMode.READ, true);
			var row:Object = result.data[i].pvalue;

			return result.data[i].pvalue;
		}
		
		/** 删除  */
		public static function deleteMusicBoxParameter():Array{
			var sql:String = "SELECT * FROM MusicBoxParameter";					
			var result:SQLResult = LocalDbConnection.sqlExecute(sql, SQLMode.READ, true);			
			var numResults:int = result.data.length;
			
			for(var i:int=0; i<numResults; i++){
				var row:Object = result.data[i];				
				trace(row.pname + ":" + row.pvalue);
			}

			return result;
		}
	}
}